﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IServices;
using Models;
using System.Data;
namespace ImpServices
{
    public class BlogServices : IBlogServices
    {
        /// <summary>
        /// 返回博客的个数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            string sql = "select * from blog order by publishTime desc";
            var count = SqlServices.SqlHelper.ExecuteScalar(sql, null);
            int result = count == null ? 0 : Convert.ToInt32(count);
            return result;
        }

        /// <summary>
        /// 获取到所有的博客数据
        /// </summary>
        /// <returns></returns>
        public List<Models.BlogInfo> GetAllBlogList()
        {
            List<BlogInfo> listBlog = new List<BlogInfo>();
            string sql = "select * from blog order by publishTime desc";
            DataTable dtBlog = SqlServices.SqlHelper.ExecuteDataTable(sql, null);
            foreach (DataRow row in dtBlog.Rows)
            {
                BlogInfo blogInfo = new BlogInfo();
                blogInfo.ID = (int)row["id"];
                blogInfo.Title = (string)row["title"];
                blogInfo.BlogContent = (string)row["blogContent"];
                blogInfo.AuthorID = row["authorID"] == DBNull.Value ? 1 : Convert.ToInt32(row["authorID"]);
                blogInfo.CategoryID = (int)row["categoryID"];
                blogInfo.LinkUrl = (string)row["linkUrl"];
                blogInfo.PublishTime = (DateTime)row["publishTime"];
                listBlog.Add(blogInfo);
            }
            return listBlog;
        
        
        
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="pageNo">分页数</param>
        /// <param name="pageCount">每页显示的个数</param>
        /// <returns></returns>
        public List<Models.BlogInfo> GetBlogListWithPageNo(int pageNo, int pageCount)
        {
            if (pageNo < 1)
            {
                pageNo = 1;
            }
            int startIndex = (pageNo - 1) * pageCount;
            List<BlogInfo> listBlog = new List<BlogInfo>();
            string sql = "select * from blog order by publishTime desc";
            DataTable dtBlog = SqlServices.SqlHelper.ExecuteDataTable(sql, null);
            foreach (DataRow row in dtBlog.Rows)
            {
                BlogInfo blogInfo = new BlogInfo();
                blogInfo.ID = (int)row["id"];
                blogInfo.Title = (string)row["title"];
                blogInfo.BlogContent = (string)row["blogContent"];
                blogInfo.AuthorID = row["authorID"]== DBNull.Value ?1:Convert.ToInt32(row["authorID"]);
                blogInfo.CategoryID = (int)row["categoryID"];
                blogInfo.LinkUrl = (string)row["linkUrl"];
                blogInfo.PublishTime = (DateTime)row["publishTime"];
                listBlog.Add(blogInfo);
            }
            return listBlog;
        }
    }
}
